# -*-coding: utf-8 -*-
"""
    @Theme   : Celery Tasks
    @Time    : 2024/7/13 11:02
    @Author  : YamFish
    @Url     : https://www.zhihu.com/people/yamfish
"""
import os
import time

import dotenv
from redis import Redis

from celery_tasks.app import celery_app
from qanda.settings import REDIS_KEY_SESSION_EXPIRE, REDIS_KEY_SESSION


dotenv.load_dotenv()
client = Redis(host=os.getenv('REDIS_HOST'), password=os.getenv('REDIS_PASSWORD'))


@celery_app.task
def delete_expired_session():
    now = time.time()
    expired_sessions = client.zrangebyscore(REDIS_KEY_SESSION_EXPIRE, 0, now)
    for session in expired_sessions:
        client.hdel(REDIS_KEY_SESSION, session)
        client.zrem(REDIS_KEY_SESSION_EXPIRE, session)
